package main

import (
	"fmt"
	"time"
)

func main() {
	// 当前时间及各个部分
	now := time.Now()
	fmt.Printf("current time: %v\n", now)
	fmt.Println(now.Year(), int(now.Month()), now.Day(), now.Hour(), now.Minute(), now.Second())
	fmt.Printf("时间戳 unix: %v, unixNano: %v\n", now.Unix(), now.UnixNano())

	// 时间戳
	num := now.Unix()
	t0 := time.Unix(num, 0)
	fmt.Println(t0)

	// 时间间隔
	t1 := t0.Add(time.Hour * 2)
	t2 := t0.Sub(t1) // 返回Duration
	fmt.Println(t1, t2)

	// 格式化与解析
	fmt.Println(now.Format(time.DateTime)) // "2006-01-02 15:04:05"

	loc, err := time.LoadLocation("Asia/Shanghai")
	t3, err := time.ParseInLocation(time.DateTime, "2025-10-10 15:00:00", loc)
	if err != nil {
		fmt.Println("解析错误")
		return
	}
	fmt.Println(t3)

	// 定时器
	ticker := time.Tick(time.Second)
	for t := range ticker {
		fmt.Println(t)
	}
}
